package aochulai.qisiren;

import baseFx.common.Stream;
import baseFx.common.StreamGroup;

import java.util.Map;

public class Rules {
    public static final int Pai_Da = 0;
    public static final int Pai_Xiao = 1;
    public static final int Pai_2 = 2;
    public static final int Pai_1 = 3;
    public static final int Pai_K = 4;
    public static final int Pai_Q = 5;
    public static final int Pai_J = 6;
    public static final int Pai_10 = 7;
    public static final int Pai_9 = 8;
    public static final int Pai_8 = 9;
    public static final int Pai_7 = 10;
    public static final int Pai_6 = 11;
    public static final int Pai_5 = 12;
    public static final int Pai_4 = 13;
    public static final int Pai_3 = 14;

    public static Integer[] YiFuPai =
            {
                    Pai_Da, Pai_Xiao,
                    Pai_2, Pai_2, Pai_2, Pai_2,
                    Pai_1, Pai_1, Pai_1, Pai_1,
                    Pai_K, Pai_K, Pai_K, Pai_K,
                    Pai_Q, Pai_Q, Pai_Q, Pai_Q,
                    Pai_J, Pai_J, Pai_J, Pai_J,
                    Pai_10, Pai_10, Pai_10, Pai_10,
                    Pai_9, Pai_9, Pai_9, Pai_9,
                    Pai_8, Pai_8, Pai_8, Pai_8,
                    Pai_7, Pai_7, Pai_7, Pai_7,
                    Pai_6, Pai_6, Pai_6, Pai_6,
                    Pai_5, Pai_5, Pai_5, Pai_5,
                    Pai_4, Pai_4, Pai_4, Pai_4,
                    Pai_3, Pai_3, Pai_3, Pai_3
            };

    /**
     * 出牌格式是否正确
     */
    public static boolean fmtcrt(Integer[] pais) {
        if (pais.length == 1) {
            return pais[0] != Pai_Da && pais[0] != Pai_Xiao;
        }
        if (xiangtong(pais)) {
            return true;
        }
        if (shunzi(pais)) {
            return pais.length >= 4;
        }
        if (liandui(pais)) {
            return true;
        }
        return false;
    }

    public static boolean eq(Integer[] pre, Integer[] last) {
        if (last.length != pre.length) {
            return false;
        }
        if (quanshi2(pre)) {
            return false;
        }
        if (quanshi2(last)) {
            return true;
        }
        if (last.length == 1) {
            return danpai(pre[0], last[0]);
        }
        if (xiangtong(pre) && xiangtong(last)) {
            return danpai(pre[0], last[0]);
        }
        if (shunzi(pre) && shunzi(last)) {
            return danpai(pre[0], last[0]);
        }
        if (liandui(pre) && liandui(last)) {
            return danpai(pre[0], last[0]);
        }
        return false;
    }

    private static boolean liandui(Integer[] pais) {
        StreamGroup<Integer> group = Stream.create(pais).group(Object::toString);
        if (group.any()) {
            int size = group.first().get().size();
            return group.all(i -> i.size() == size) && group.count() > 1;
        }
        return false;
    }

    private static boolean xiangtong(Integer[] pais) {
        return Stream.create(pais).all(i -> i.equals(pais[0]));
    }

    private static boolean danpai(Integer p1, Integer p2) {
        if (p1 == Pai_2) {
            return false;
        }
        if (p2 == Pai_2) {
            return true;
        }
        return (p1 - 1) == p2;
    }

    private static boolean shunzi(Integer[] pais) {
        pais = Stream.create(pais).sort().toArray(Integer.class);
        for (int i = 0; i < pais.length - 1; i++) {
            if (pais[i] == Pai_2)
                return false;
            if (pais[i].equals(pais[i + 1] - 1) == false)
                return false;
        }
        return true;
    }

    private static boolean quanshi2(Integer[] pais) {
        return Stream.create(pais).all(i -> i == Pai_2);
    }
}
